package com.asset.dispoal.file.replyDetail.domain.service.command;

import com.asset.dispoal.file.replyDetail.domain.db.mapper.FileReplyDetailMapper;
import com.asset.dispoal.file.replyDetail.dto.FileReplyRequest;
import com.asset.dispoal.file.replyDetail.dto.FileReplyUserRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.springboot.protocol.Result;

@Component
public class FileReplyModifyCommand implements BaseCommand<FileReplyRequest, Result> {

    @Autowired
    private FileReplyDetailMapper fileReplyDetailMapper;

    @Autowired
    private BaseIdeable<String> ideable;

    @Override
    public Result execute(FileReplyRequest request) {
        fileReplyDetailMapper.deleteUser(request.getId());
        if (request.getUserList() != null) {
            for (FileReplyUserRequest user : request.getUserList()) {
                if (user.getId() == null) {
                    user.setId(ideable.generateId());
                }
            }
        }
        fileReplyDetailMapper.modify(request);

        return Result.ok();
    }
}
